package cn.com.shopec.erp.financing.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.com.shopec.erp.common.constant.SZSCMConstant;
import cn.com.shopec.erp.common.page.PageFinder;
import cn.com.shopec.erp.common.page.PageQuery;
import cn.com.shopec.erp.common.utils.StringUtil;
import cn.com.shopec.erp.financing.dao.IWhareHouseDao;
import cn.com.shopec.erp.financing.entity.WhareHouse;
import cn.com.shopec.erp.financing.service.IWhareHouseServer;

/**
 * 仓库类业务接口
 * @author xiaoyunfei
 *
 */
@Service
public class WhareHouseServerImpl implements IWhareHouseServer {
	
	@Resource 
	private IWhareHouseDao iWhareHouseDao;
	/**
	 * 获取所有仓库实体
	 * @return
	 */
	@Override
	public List<WhareHouse> getAllWhareHouse() throws Exception {
		DetachedCriteria criteria = iWhareHouseDao.createCriteria();
		criteria.add(Restrictions.eq("deleteFlag", SZSCMConstant.DELETE_FLAG_NO));
		return iWhareHouseDao.findByCriteria(criteria);
	}
	/**
	 * 查询虚库
	 * @return
	 */
	@Override
	public List<WhareHouse> getAllWhareTypeTwo() throws Exception {
		DetachedCriteria criteria = iWhareHouseDao.createCriteria();
		criteria.add(Restrictions.eq("deleteFlag", SZSCMConstant.DELETE_FLAG_NO));
		criteria.add(Restrictions.eq("whType", SZSCMConstant.INVENTORYTYPE_2));
		List<WhareHouse> lists = iWhareHouseDao.findByCriteria(criteria);		
		return lists;
	}
	/**
	 * 通过仓库编码获取仓库实体
	 * @return
	 */
	@Override
	public WhareHouse getWhareHouseByNo(String weNo) {		
		DetachedCriteria criteria = iWhareHouseDao.createCriteria();
		criteria.add(Restrictions.eq("deleteFlag", SZSCMConstant.DELETE_FLAG_NO));
		criteria.add(Restrictions.eq("whNo", weNo));
		 List<WhareHouse> lists =iWhareHouseDao.findByCriteria(criteria);
		 if(null!=lists&&lists.size()>0){
			 return lists.get(0);
		 }
		return null;
	}
	/**
	 * 分布查询仓库信息
	 */
	@Override
	public PageFinder<WhareHouse> getPagedCollectSetList(PageQuery pageQuery) {
		 DetachedCriteria criteria = iWhareHouseDao.createCriteria();    
    	 //数据有效。。未被删除
    	 criteria.add(Restrictions.eq("deleteFlag", SZSCMConstant.DELETE_FLAG_NO)); 
    	 PageFinder<WhareHouse>  pf= iWhareHouseDao.pagedByCriteria(criteria, pageQuery.getPage(), pageQuery.getPageSize());    	
		return pf;
	}
	@Transactional
	@Override
	public void updateWhareHouse(WhareHouse whareHouse) throws Exception {
		iWhareHouseDao.update(whareHouse);	
	}
	@Override
	public boolean findWhareHouseByName(String id, String whName) {
		
		List<WhareHouse> lists  = null;
		if(!StringUtil.isEmpty(id)){
			DetachedCriteria detachedCriteria = iWhareHouseDao.createCriteria();
			detachedCriteria.add(Restrictions.eq("whName", whName));
			detachedCriteria.add(Restrictions.not(Restrictions.eq("id", id)));
			detachedCriteria.add(Restrictions.eq("deleteFlag", SZSCMConstant.DELETE_FLAG_NO));
			lists = iWhareHouseDao.findByCriteria(detachedCriteria);		
			if(null!=lists&&lists.size()>0){
				return false;
			}
			return true;
		}else{
			DetachedCriteria detachedCriteria = iWhareHouseDao.createCriteria();
			detachedCriteria.add(Restrictions.eq("whName", whName));
			detachedCriteria.add(Restrictions.eq("deleteFlag", SZSCMConstant.DELETE_FLAG_NO));
			lists = iWhareHouseDao.findByCriteria(detachedCriteria);		
			if(null!=lists&&lists.size()>0){
				return false;
			}
			return true;
		}
		
	}
	@Override
	public boolean findWhareHouseByNo(String id, String whNo) {
		
		List<WhareHouse> lists  = null;
		if(!StringUtil.isEmpty(id)){
			DetachedCriteria detachedCriteria = iWhareHouseDao.createCriteria();
			detachedCriteria.add(Restrictions.eq("whNo", whNo));
			detachedCriteria.add(Restrictions.not(Restrictions.eq("id", id)));
			detachedCriteria.add(Restrictions.eq("deleteFlag", SZSCMConstant.DELETE_FLAG_NO));
			lists = iWhareHouseDao.findByCriteria(detachedCriteria);		
			if(null!=lists&&lists.size()>0){
				return false;
			}
			return true;
		}else{
			DetachedCriteria detachedCriteria = iWhareHouseDao.createCriteria();
			detachedCriteria.add(Restrictions.eq("whNo", whNo));
			detachedCriteria.add(Restrictions.eq("deleteFlag", SZSCMConstant.DELETE_FLAG_NO));
			lists = iWhareHouseDao.findByCriteria(detachedCriteria);		
			if(null!=lists&&lists.size()>0){
				return false;
			}
			return true;
		}
		
	}
	/**
	 * 删除仓库信息
	 * @throws Exception 
	 */
	@Transactional
	@Override
	public void deleteWhareHouse(String whNo) throws Exception {
		
		WhareHouse whareHouse = getWhareHouseByNo(whNo);		
		whareHouse.setDeleteFlag(SZSCMConstant.DELETE_FLAG_YES);
		iWhareHouseDao.update(whareHouse);
			
	}
	/**
	 * 新增仓库信息
	 * @throws Exception 
	 */
	@Transactional
	@Override
	public void addWhareHouse(WhareHouse whareHouse) throws Exception {
		// TODO Auto-generated method stub
		whareHouse.setDeleteFlag(SZSCMConstant.DELETE_FLAG_NO);
		iWhareHouseDao.save(whareHouse);
		
	}
	@Override
	public WhareHouse getWhareHouseByID(String whNo) {
		// TODO Auto-generated method stub
		try {
			return iWhareHouseDao.getById(whNo);
		} catch (Exception e) {
			return null;
		}
	}



}
